package com.atguigu.spzx.manager.service.Impl;

import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.order.OrderStatistics;
import com.atguigu.spzx.manager.mapper.OrderInfoStatisticeMapper;
import com.atguigu.spzx.manager.service.OrderInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LT
 * @create 2023-11-12-18:58
 */
@Service
public class OrderInfoServicempl implements OrderInfoService {

    @Resource
    OrderInfoStatisticeMapper orderInfoStatisticeMapper;

    @Override
    public Map<String, Object> findOrderStatistics(String begin, String end) {
            /*
        即时查询：数据量较少的表 通过表数据统计分析查询结果直接返回
            查询每天的 统计数据  订单总金额  订单总数量
            1、前端图表组件需要的数据格式

                x轴： 日期列表
                每个日期的值：  订单总金额列表    订单总数量

            2、后端查询到的数据如何组装
                > List<Date> 日期列表
                > List<BigDecimal>     订单总金额列表
                > List<Integer>     订单总数量

                SELECT DATE(create_time) , SUM(total_amount) , count(1)
                FROM order_info
                WHERE is_deleted = 0

                GROUP BY DATE(create_time)
                ORDER BY DATE(create_time);

                自定义javabean：接收sql查询到的统计分析数据返回
         */
        List<OrderStatistics> orderStatisticsList = orderInfoStatisticeMapper.findOrderStatistics(begin,end);
        List<Date> dates = orderStatisticsList.stream()
                .map(OrderStatistics::getOrderDate)
                .collect(Collectors.toList());

        List<BigDecimal> totalAmounts = orderStatisticsList.stream()
                .map(OrderStatistics::getTotalAmount)
                .collect(Collectors.toList());

        List<Integer> totalNums = orderStatisticsList.stream()
                .map(OrderStatistics::getTotalNum)
                .collect(Collectors.toList());

        Map<String,Object> map = new HashMap<>();
        map.put("dates",dates);
        map.put("totalAmounts",totalAmounts);
        map.put("totalNums",totalNums);
        return map;
    }
}
